<?php

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Repositories\User\UserRepository;
use Illuminate\Support\Facades\Request;
use App\Repositories\System\UserActionLog;
use Illuminate\Support\Facades\Auth;

class UserEventListener {

	protected $UserRepository;

	public function __construct(UserRepository $UserRepository, UserActionLog $logger) {

		$this->UserRepository = $UserRepository;
		$this->logger         = $logger;
		$this->user           = Auth::user();

	}

	/**
	 * 用户登录
	 * @param $event
	 */
	public function onUserLogin($event) {
		$user = $event->user;
		//记录登录时间与ip
		$this->UserRepository->update([
			'login_time' => time(),
			'login_ip'   => Request::getClientIp(),
		], $user->id);
		//记录日志
		$this->logger->record('user','login');
	}

	/**
	 * 用户注册
	 * @param $event
	 */
	public function onUserRegister($event) {

		$user = $event->user;

	}


	/**
	 * 事件订阅
	 * @param $events
	 */
	public function subscribe($events) {
		$events->listen(
			'App\Events\User\UserLogin',
			'App\Listeners\UserEventListener@onUserLogin'
		);

		$events->listen(
			'App\Events\User\UserRegister',
			'App\Listeners\UserEventListener@onUserRegister'
		);
	}

}
